package com.fone.player.activity.pay;


import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;

import com.fone.player.R;

public class PayResultDialog implements OnClickListener{
	private static final String TAG = "SpeakerPopupWindow";

	private View mView;
	private Context mContext;
	private PopupWindow mPopupWindow;
	private AlPayResltListener mListener;
	private TextView tv_pay_msg,wx_button_tv,tv_wx_pay_title;
	
	public PayResultDialog(Context context,AlPayResltListener alPayResltListener){
		mView = View.inflate(context, R.layout.pay_result, null);
		mContext = context;
		mListener = alPayResltListener;
		
		if (mPopupWindow == null) {
			mPopupWindow = new PopupWindow(mView, LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT);

			mPopupWindow.setWidth(LayoutParams.WRAP_CONTENT);
			mPopupWindow.setHeight(LayoutParams.WRAP_CONTENT);// 设置弹出框大小
			// 设置焦点
			mPopupWindow.setFocusable(true);
			// 设置点击其他地方 无法消失
			mPopupWindow.setOutsideTouchable(false);
		}
		initView();
		mPopupWindow.setOnDismissListener(new OnDismissListener() {
			@Override
			public void onDismiss() {
				if(mListener!=null){
					mListener.onDismiss();
				}
			}
		});
	}

	private void initView() {
		tv_pay_msg = (TextView) mView.findViewById(R.id.tv_wx_pay_msg);
		wx_button_tv = (TextView)mView.findViewById(R.id.tv_wx_button);
		tv_wx_pay_title = (TextView) mView.findViewById(R.id.tv_wx_pay_title);
		
		tv_wx_pay_title.setText("支付宝支付结果");
		wx_button_tv.setOnClickListener(this);
	}
	
	public interface AlPayResltListener{
		public void onDismiss();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_wx_button:
			dismiss(v);
			break;

		default:
			break;
		}
		
	}
	
	public void setListner(AlPayResltListener alPayResltListener){
		mListener = alPayResltListener;
	}
	
	/**
	 * 展示PopWindow
	 * 
	 * @param view
	 */
	public void showPopWindow(View view,String msg) {
		tv_pay_msg.setText(msg);
		if (null == mPopupWindow) {
			mPopupWindow = new PopupWindow(mView, LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT);
			// 设置焦点
			mPopupWindow.setFocusable(true);
			// 设置点击其他地方 无法消失
			mPopupWindow.setOutsideTouchable(false);
		}
		mPopupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
	}

	
	public void dismiss(View v) {
		if (mPopupWindow != null && mPopupWindow.isShowing()) {
			mPopupWindow.dismiss();
		}
	}

}
